package org.dromara.mall.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.mall.domain.bo.TerMallCartWxBo;
import org.dromara.mall.domain.vo.TerMallProductVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.mall.domain.vo.TerMallCartVo;
import org.dromara.mall.domain.bo.TerMallCartBo;
import org.dromara.mall.service.ITerMallCartService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 购物车
 * 前端访问路由地址为:/mall/mallCart
 *
 * @author xch
 * @date 2025-06-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mallCart")
public class TerMallCartController extends BaseController {

    private final ITerMallCartService terMallCartService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/wechat/list")
    public R<List<TerMallProductVo>> list(TerMallCartBo bo) {
        return R.ok(terMallCartService.queryProductList(bo));
    }


    /**
     * 新增购物车
     */

    @RepeatSubmit()
    @PostMapping("/wechat")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerMallCartBo bo) {
        try{
             toAjax(terMallCartService.insertByBo(bo));
             return R.ok();
        }catch (Exception e) {
            return  R.warn(e.getMessage());
        }
    }

    /**
     * 修改购物车
     */

    @RepeatSubmit()
    @PutMapping("/wechat/update")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerMallCartWxBo bo) {
        try{
            toAjax(terMallCartService.updateByBo(bo.getCartList()));
            return R.ok();
        }catch (Exception e) {
            return R.warn(e.getMessage());
        }
    }

    /**
     * 删除购物车
     *
     * @param id 主键串
     */
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("id") String id) {
        return toAjax(terMallCartService.deleteWithValidById(id));
    }
}
